/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function (nums) {
    // 将数组从小到大排序
    nums.sort((a, b) => a - b)
    const ans = []
    let left = 0
    while (left < nums.length - 2) {
        if (left !== 0 && nums[left] == nums[left - 1]) {
            left++
            continue
        }
        let temp = left + 1
        let right = nums.length - 1
        if(nums[left] > 0) break
        while (temp < right) {
            if (nums[left] + nums[temp] + nums[right] === 0) {
                ans.push([nums[left], nums[temp], nums[right]])
                temp++
                right--
            } else if (nums[left] + nums[temp] + nums[right] > 0) {
                right--
            } else {
                temp++
            }
            while (temp !== (left + 1) && nums[temp] == nums[temp - 1]) temp++
            while (right !== nums.length - 1 && nums[right] == nums[right + 1]) right--
        }
        left++
    }
    return ans
}
console.log(threeSum([-2,0,1,1,2]));
console.log(threeSum([-1,0,1,2,-1,-4,-2,-3,3,0,4]));